// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.ui;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.HashMap;
import zombie.core.logger.ExceptionLogger;
import zombie.core.math.PZMath;
import zombie.debug.DebugLog;
import zombie.scripting.ScriptParser;
import zombie.util.StringUtils;

public final class FontsFile {
    private static final int VERSION1 = 1;
    private static final int VERSION = 1;

    public boolean read(String path, HashMap<String, FontsFileFont> fonts) {
        try {
            FileReader fileReader = new FileReader(path);

            boolean _boolean;
            try {
                BufferedReader bufferedReader = new BufferedReader(fileReader);

                try {
                    StringBuilder stringBuilder = new StringBuilder();

                    for (String string = bufferedReader.readLine(); string != null; string = bufferedReader.readLine()) {
                        stringBuilder.append(string);
                    }

                    this.fromString(stringBuilder.toString(), fonts);
                    _boolean = true;
                } catch (Throwable throwable0) {
                    try {
                        bufferedReader.close();
                    } catch (Throwable throwable1) {
                        throwable0.addSuppressed(throwable1);
                    }

                    throw throwable0;
                }

                bufferedReader.close();
            } catch (Throwable throwable2) {
                try {
                    fileReader.close();
                } catch (Throwable throwable3) {
                    throwable2.addSuppressed(throwable3);
                }

                throw throwable2;
            }

            fileReader.close();
            return _boolean;
        } catch (FileNotFoundException fileNotFoundException) {
            return false;
        } catch (Exception exception) {
            ExceptionLogger.logException(exception);
            return false;
        }
    }

    private void fromString(String string, HashMap<String, FontsFileFont> hashMap) {
        string = ScriptParser.stripComments(string);
        ScriptParser.Block block0 = ScriptParser.parse(string);
        int _int = -1;
        ScriptParser.Value value0 = block0.getValue("VERSION");
        if (value0 != null) {
            _int = PZMath.tryParseInt(value0.getValue(), -1);
        }

        if (_int >= 1 && _int <= 1) {
            for (ScriptParser.Block block1 : block0.children) {
                if (!block1.type.equalsIgnoreCase("font")) {
                    throw new RuntimeException("unknown block type \"" + block1.type + "\"");
                }

                if (StringUtils.isNullOrWhitespace(block1.id)) {
                    DebugLog.General.warn("missing or empty font id");
                } else {
                    ScriptParser.Value value1 = block1.getValue("fnt");
                    ScriptParser.Value value2 = block1.getValue("img");
                    if (value1 != null && !StringUtils.isNullOrWhitespace(value1.getValue())) {
                        FontsFileFont fontsFileFont = new FontsFileFont();
                        fontsFileFont.id = block1.id;
                        fontsFileFont.fnt = value1.getValue().trim();
                        if (value2 != null && !StringUtils.isNullOrWhitespace(value2.getValue())) {
                            fontsFileFont.img = value2.getValue().trim();
                        }

                        hashMap.put(fontsFileFont.id, fontsFileFont);
                    } else {
                        DebugLog.General.warn("missing or empty value \"fnt\"");
                    }
                }
            }
        } else {
            throw new RuntimeException("invalid or missing VERSION");
        }
    }
}
